package com.it.controller;

import com.github.pagehelper.PageInfo;
import com.it.pojo.*;
import com.it.pojo.dto.InStoreDto;
import com.it.pojo.dto.ProductDto;
import com.it.pojo.dto.TimeDto;
import com.it.service.ContainerService;
import com.it.service.InStoreService;
import com.it.service.ProductService;
import com.it.service.UserInfoService;
import com.it.util.ExcelUtils;
import com.it.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/instore")
public class InstoreController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ContainerService containerService;
    @Autowired
    private InStoreService inStoreService;

    @GetMapping("/topage")
    public String toinstore(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        List<Product> product = productService.getProducts(userInfoService.getOneUserInfo(user.getUserId()).getCompanyId());
        request.setAttribute("productlist",product);
        System.out.println(product);
        return "pages/instore/instore";
    }

    @GetMapping("/getproduct/{productid}")
    @ResponseBody
    public Product findproduct(@PathVariable("productid") String pid){
        return productService.getProduct(pid);
    }

    @PostMapping("/doinstore")
    @ResponseBody
    public R<String> doinstore(InStore inStore, HttpSession session){
        System.out.println(inStore);
        User user = (User)session.getAttribute("user");
        inStore.setUserId(user.getUserId());
        inStore.setUserName(user.getUserLoginname());

        Container oneContainer = containerService.getOneContainer(inStore.getContainerId());
        inStore.setInNum(Double.parseDouble(oneContainer.getContainerRealvolume().toString()));
        inStore.setInUnit(oneContainer.getContainerUnit());

        UserInfo oneUserInfo = userInfoService.getOneUserInfo(user.getUserId());
        inStore.setCompanyId(oneUserInfo.getCompanyId());

        String msg = inStoreService.instore(inStore);
        return R.error(msg);
    }

    @GetMapping("/find")
    public ModelAndView find(InStoreDto dto){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/instore/list");
        PageInfo<InStore> pageInfo = inStoreService.findPage(dto);
        modelAndView.addObject("pageInfo",pageInfo);
        List<UserInfo> users = userInfoService.findAll();
        modelAndView.addObject("users",users);
        List<TimeDto> dtoList = new ArrayList<>();
        dtoList.add(new TimeDto(1,"当天"));
        dtoList.add(new TimeDto(2,"本月"));
        dtoList.add(new TimeDto(3,"本季"));
        dtoList.add(new TimeDto(4,"本年"));
        modelAndView.addObject("timeList",dtoList);
        return modelAndView;
    }

    @PostMapping("/find")
    public ModelAndView one(InStoreDto dto){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/instore/list");
        PageInfo<InStore> pageInfo = inStoreService.findPage(dto);
        modelAndView.addObject("pageInfo",pageInfo);
        List<UserInfo> users = userInfoService.findAll();
        modelAndView.addObject("users",users);
        List<TimeDto> dtoList = new ArrayList<>();
        dtoList.add(new TimeDto(1,"当天"));
        dtoList.add(new TimeDto(2,"本月"));
        dtoList.add(new TimeDto(3,"本季"));
        dtoList.add(new TimeDto(4,"本年"));
        modelAndView.addObject("timeList",dtoList);
        return modelAndView;
    }

    @PostMapping("/excel")
    public ModelAndView excel(InStoreDto dto, HttpServletResponse response) throws Exception {
        System.out.println(dto);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/instore/find");
        /*导入excel操作*/
        List<InStore> excel = inStoreService.findExcel(dto);
        ExcelUtils.export(response,"入库记录",excel,InStore.class);
        return modelAndView;
    }


}
